package com.ticket.sass.admin.business.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ticket.sass.admin.business.OrderCardRechargesBusiness;
import com.ticket.sass.admin.dto.input.order.OrderCardRechargeListInput;
import com.ticket.sass.admin.dto.output.order.OrderCardRechargesListOutput;
import com.ticket.sass.admin.dto.output.order.OrderDataModel;
import com.ticket.sass.admin.service.impl.MemberCardServiceImpl;
import com.ticket.sass.admin.service.impl.OrderCardRechargeServiceImpl;
import com.ticket.sass.admin.service.impl.OrderServiceImpl;
import com.ticket.sass.admin.service.impl.UserServiceImpl;
import com.ticket.sass.admin.util.ModelUtil;
import com.ticket.sass.common.entity.tenant.MemberCardEntity;
import com.ticket.sass.common.entity.tenant.OrderCardRechargeEntity;
import com.ticket.sass.common.entity.tenant.OrderEntity;
import com.ticket.sass.common.entity.tenant.UserEntity;
import com.ticket.sass.common.entity.tenant.enums.OrderType;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author ywb
 * @date 2024-03-14 11:18
 */
@Slf4j
@Service
@AllArgsConstructor
public class OrderCardRechargesBusinessImpl implements OrderCardRechargesBusiness {
    final ModelUtil modelUtil;
    final UserServiceImpl userService;
    final OrderCardRechargeServiceImpl service;
    final OrderServiceImpl orderService;
    final MemberCardServiceImpl memberCardService;

    
    @Override
    public OrderDataModel<OrderCardRechargesListOutput> list(OrderCardRechargeListInput input) {
        List<Long> inputUserIds = userService.getUserIdByCellphone(input.getCellphone());
        if (StrUtil.isNotEmpty(input.getCellphone()) && inputUserIds.isEmpty()){
            return new OrderDataModel<>();
        }
        input.setUserIds(inputUserIds);
     
        IPage<OrderCardRechargeEntity> page = service.iPage(input);
        List<OrderCardRechargesListOutput> outputs = page.getRecords().stream().map(OrderCardRechargesListOutput::new).collect(Collectors.toList());

        // 提取用户ID列表
        List<Long> userIds = outputs.stream()
            .map(OrderCardRechargesListOutput::getUserId)
            .collect(Collectors.toList());
        
        Map<Long, UserEntity> longUserEntityMap = userService.mapByIds(userIds);
        
        // 提取会员卡ID列表
        List<Long> cardIds = outputs.stream()
            .map(OrderCardRechargesListOutput::getCardId)
            .collect(Collectors.toList());
        
        Map<Long, MemberCardEntity> longMemberCardEntityMap = memberCardService.mapByIds(cardIds);
        
        
        outputs.forEach(o -> {
            o.setCardNumber(longMemberCardEntityMap.getOrDefault(o.getCardId(), new MemberCardEntity()).getCardNumber());
            o.setUsername(longUserEntityMap.getOrDefault(o.getUserId(), new UserEntity()).getNickname());
            o.setCellphone(longUserEntityMap.getOrDefault(o.getUserId(), new UserEntity()).getCellphone());
        });
        
        OrderDataModel orderDataModel = new OrderDataModel();
        
        orderDataModel.setTotal(page.getTotal());
        orderDataModel.setList(outputs);
        
        List<Long> longs = service.orderIds(input);
        
        OrderEntity order = orderService.dateCount(OrderType.CARD_RECHARGE.getValue(), null, longs);
 
        if (Objects.nonNull(order)){
            orderDataModel.setSumPayPrice(order.getSumPayPrice());
            orderDataModel.setWxSumPayPrice(order.getWxSumPayPrice());
            orderDataModel.setMemberSumPayPrice(order.getMemberSumPayPrice());
        }
        return orderDataModel;
    }
}
